package tool;

import constant.ConfigConstants;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * Created by Intellij IDEA.
 * User:  老赵
 * Date:  2023/7/19
 */
public class ASCII {

    private static TextArea asciiDataField;

    public static void openASCIIStage() {
        Stage stature = new Stage();
        stature.setTitle("ASCII转换");
        stature.setFullScreen(ConfigConstants.FullScreen);

        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10)); // 设置内边距为10像素
        gridPane.setVgap(10); // 设置垂直间距为10像素
        gridPane.setHgap(10); // 设置水平间距为10像素

        Label trackingNumberLabel = new Label("ASCII转换:");
        gridPane.addRow(0, trackingNumberLabel);
        asciiDataField = new TextArea();
        asciiDataField.setWrapText(true); // 自动换行
        asciiDataField.setMinWidth(400);
        asciiDataField.setMinHeight(100);
        asciiDataField.setText("支持ASCII编码与字符的相互转换");
        gridPane.addRow(1, asciiDataField);

        Button turn = new Button("字符转换ASCII");
        Button anti = new Button("ASCII转换字符");
        gridPane.addRow(2, turn);
        gridPane.addRow(3, anti);

        turn.setOnAction(event -> {
            String data = asciiDataField.getText();
            //判断是否是ASCII
            if (isASCII(data)) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("警告");
                alert.setHeaderText(null);
                alert.setContentText("请输入正确的字符");
                alert.showAndWait();
                return;
            }
            String result = turn(data);
            asciiDataField.setText(result);
        });

        anti.setOnAction(event -> {
            String data = asciiDataField.getText();
            //判断是否是ASCII
            if (!isASCII(data)) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("警告");
                alert.setHeaderText(null);
                alert.setContentText("请输入正确的ASCII");
                alert.showAndWait();
                return;
            }
            String result = anti(data);
            asciiDataField.setText(result);
        });

        Scene scene = new Scene(gridPane, 500, 700);
        stature.setScene(scene);
        stature.show();
    }

    //字符转ASCII
    public static String turn(String data) {
        StringBuilder unicode = new StringBuilder();
        for (char c : data.toCharArray()) {
            unicode.append("\\u").append(Integer.toHexString(c));
        }
        return unicode.toString();
    }

    //ASCII转字符
    public static String anti(String data) {
        StringBuilder chinese = new StringBuilder();
        String[] unicodeArray = data.split("\\\\u");
        for (int i = 1; i < unicodeArray.length; i++) {
            int unicodeValue = Integer.parseInt(unicodeArray[i], 16);
            chinese.append((char) unicodeValue);
        }
        return chinese.toString();
    }

    //判断用户输入的是字符还说ASCII
    public static boolean isASCII(String data) {
        boolean isASCII = true;
        for (int i = 0; i < data.length(); i++) {
            if ((int) data.charAt(i) > 127) {
                isASCII = false;
                break;
            }
        }
        return isASCII;
    }
}
